from rest_framework import serializers
from testapp.models import *


class ClassesRelateField(serializers.RelatedField):

    def to_representation(self, value):
        return {'id': value.id, 'name': value.name}


class StudentSerializer(serializers.ModelSerializer):
    classes = ClassesRelateField(read_only=True)

    class Meta:
        model = Student
        fields = ['id', 'name', 'age', 'sex', 'classes', 'address']


class ClassesSerializer(serializers.ModelSerializer):
    students = StudentSerializer(many=True, read_only=True)

    class Meta:
        model = Classes
        fields = ['id', 'name', 'students']


class StudentInfoSerializer(serializers.ModelSerializer):
    classes = ClassesRelateField(read_only=True)

    class Meta:
        model = StuApplication
        fields = ['id', 'student', 'application_classes', 'application_Text', 'application_time']


class TeachersSerializer(serializers.ModelSerializer):

    classes = ClassesRelateField(many=True, read_only=True)

    class Meta:
        model = Teachers
        fields = ['id', 'teacher_name', 'classes']


class ClassSerializer(serializers.ModelSerializer):

    class Meta:
        model = Classes
        fields = ['id', 'name']
